package com.offcn.service.impl;

import com.offcn.entity.Address;
import com.offcn.entity.User;
import com.offcn.mapper.AddressMapper;
import com.offcn.mapper.UserMapper;
import com.offcn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AddressMapper addressMapper;

    @Override
    public int add(User user) {
        userMapper.add(user);
        for (Address address:user.getAddresss()) {
            address.setUid(user.getId());
            addressMapper.add(address);
        }
        return 1;
    }


    @Override
    public int delete(int id) {

        addressMapper.delete(id);
        userMapper.delete(id);
        return 1;
    }


    @Override
    public int update(Address address) {
        int aid = address.getAid();
        int uid = address.getUid();
        String address1 = address.getAddress();
        addressMapper.update(aid,uid,address1);
        return 1;
    }

    @Override
    public List<User> select() {
        List<User> users = userMapper.select();
        return users;
    }


}
